package com.spring.controller;

import com.spring.pojo.Users;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("users")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/addUser")
    public String addUser(Users users) {
        userService.addUser(users);
        return "redirect:/users/listUser";
    }
    @RequestMapping("/del")
    public String del(Integer id) {
        userService.del(id);
        return "redirect:/users/listUser";
    }


    @RequestMapping("/adds")
    public String add() {
        return "add";
    }

    @RequestMapping("/listUser")
    public String listUser(Model model) {
        List<Users> list = this.userService.listUser();
        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/login")
    public String login(Model model, Users users, HttpSession session) {
        Users login = userService.login(users);
        if (login != null) {
            session.setAttribute("user", users);
            return "redirect:/users/listUser";
        }
        return "add";
    }

    @RequestMapping("/dl")
    public String showRegister(){
        return "login";
    }
    @RequestMapping("/list")
    @ResponseBody
    public List<Users> list() {
        return   userService.list();
    }
}
